{% extends "base/base.html" %}
{% block title %}
Registries | Breezes - Docker Registry 管理平台
{% endblock %}
{% block container %}
{% set registries = g.registries.getRegistries() %}
{% set ActiveRegistry = g.registries.getActive %}
{% if not registries %}
<div class="alert alert-warning" role="alert"><i class="icon-info-sign"></i> 当前系统无仓库!!!</div>
{% endif %}
{% if not ActiveRegistry %}
<div class="alert alert-danger" role="alert"><i class="icon-remove-sign"></i> 当前系统无活跃仓库!!!</div>
{% endif %}
<p>
    <a href="{{ url_for('ui.index') }}"><button type="button" class="btn btn-default"><i class="icon-repeat"></i> Reload</button></a>
    <a href="javascript:RegistryAdd()"><button type="button" class="btn btn-success">添加仓库</button></a>
</p>
<table class="table table-bordered table-hover table-condensed table-responsive">
    <thead>
        <tr class="active">
            <th style="vertical-align: middle; text-align: center;">isActive</th>
            <th style="vertical-align: middle; text-align: center;">Name</th>
            <th style="vertical-align: middle; text-align: center;">Address</th>
            <th style="vertical-align: middle; text-align: center;">Version</th>
            <th style="vertical-align: middle; text-align: center;">State</th>
            <th style="vertical-align: middle; text-align: center;">Operations</th>
        </tr>
    </thead>
    <tbody>
    {% for registry in registries %}
        <tr>
            {% set state = g.registries._checkStatus(registry.addr, registry.version) %}
            <td style="vertical-align: middle; text-align: center;">
              {%- if registry.name == ActiveRegistry.name %}
                <attr title="当前为活跃仓库" class="icon-ok icon-large text-success"></attr>
              {% else %}
                <a href='javascript:setActive("{{ registry.name }}")'><attr title='升级为活跃仓库' class='icon-arrow-up icon-large text-primary'></attr></a>
              {% endif -%}
            </td>
            <td style='vertical-align: middle; text-align: center;'>{{ registry.name }}</td>
            <td style='vertical-align: middle; text-align: center;'><code>{{ registry.addr }}</code></td>
            <td style='vertical-align: middle; text-align: center;'><kbd>v{{ registry.version }}</kbd></td>
            {% if state %}
                <td style='vertical-align: middle; text-align: center;' class="text-success">Healthy</td>
            {% else %}
                <td style='vertical-align: middle; text-align: center;' class="text-danger">Unhealthy</td>
            {% endif %}
            <td style='vertical-align: middle; text-align: center;'>
                &nbsp;&nbsp;<a class="btn btn-xs btn-danger" href="javascript:RegistryDelete('{{ registry.name }}')"><i class="icon-trash icon-large"></i>&nbsp;删除</a>
                <br/>
            </td>
        </tr>
    {% endfor %}
    </tbody>
</table>
{% endblock %}
{% block script %}
<script type="text/javascript">
// 更新导航
var NavId=null;
var As=document.getElementById('tab').getElementsByTagName('li');
NavId = As[0];
console.log(NavId)
NavId.className='active';

// 设置活跃仓库
function setActive(name) {
    $.ajax({
        url: "{{ url_for('api.registries') }}?setActive=true&name="+name,
        method: 'PUT',
        datatype: 'json',
        success: function(res) {
            console.log(res);
            if (res.success==true) {
                layer.msg("设置活跃仓库成功！", {icon: 1});
                window.location.reload();
            } else {
                layer.msg("设置活跃仓库失败！", {icon: 2, time: 10*1000});
            }
        },
        error: function(error){
            console.log(error);
        }
    });
}

// 询问框、iframe层删除单个registry
function RegistryDelete(name) {
    layer.confirm('你确定要删除此仓库?', {
        btn: ['确认删除','我再想想']
    }, function(){
        console.warn("将要删除仓库"+name);
        if (name) {
            $.ajax({
                url: "{{ url_for('api.registries') }}",
                method: 'DELETE',
                async: false,
                datatype: 'json',
                data: {"name": name},
                success: function(res) {
                    console.log(res);
                    if (res.success==true) {
                        layer.msg("Delete Registry Successfully", {icon: 1, time: 3*1000});
                        document.location.reload(); //重载当前页面
                    } else {
                        layer.msg("Delete Registry Failed. "+res.msg, {icon: 2, time: 10*1000});
                    }
                },
            });
        };
    }, function() {
        console.log("canel");
    });
}

// iframe层添加仓库
function RegistryAdd() {
    layer.open({
        type: 2,
        title: '添加私有仓',
        shadeClose: true,
        shade: 0.8,
        area: ['800px', '90%'],
        content: '{{ url_for("ui.registry_add") }}'
    });
}
</script>
{% endblock %}
